package com.washmadam.service.service.order.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.washmadam.service.common.enums.DeleteEnum;
import com.washmadam.service.common.enums.OrderStatusEnum;
import com.washmadam.service.common.enums.ReserveEnableEnum;
import com.washmadam.service.common.enums.ReserveStatusEnum;
import com.washmadam.service.common.enums.StatusEnum;
import com.washmadam.service.common.utils.DateUtils;
import com.washmadam.service.db.dao.CustomerOrderMapper;
import com.washmadam.service.db.dto.CustomerOrder;
import com.washmadam.service.db.example.CustomerOrderExample;
import com.washmadam.service.db.example.CustomerOrderExample.Criteria;
import com.washmadam.service.db.request.query.CustomerOrderQueryParam;
import com.washmadam.service.service.order.CustomerOrderQueryService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CustomerOrderQueryServiceImpl implements CustomerOrderQueryService {
	@Autowired
	private CustomerOrderMapper customerOrderMapper;
	@Override
	public CustomerOrder getCustomerOrderById(Long customerOrderId) {
		return customerOrderMapper.selectByPrimaryKey(customerOrderId);
	}

	@Override
	public PageInfo<CustomerOrder> getCustomerOrderList(CustomerOrderQueryParam requestParam) {
		PageHelper.startPage(requestParam.getPageNum(), requestParam.getPageSize());
		CustomerOrderExample example = new CustomerOrderExample();
		Criteria criteria = example.createCriteria();
		//客户姓名
		if(StringUtils.isNotBlank(requestParam.getCustomerName())) {
			criteria.andCustomerNameLike("%"+requestParam.getCustomerName()+"%");
		}
		//客户手机号
		if(StringUtils.isNotBlank(requestParam.getContactMobile())) {
			criteria.andContactMobileLike("%"+requestParam.getContactMobile()+"%");
		}
		//客户城市
		if(StringUtils.isNotBlank(requestParam.getCity())) {
			criteria.andCityLike("%"+requestParam.getCity()+"%");
		}
		//客户地址
		if(StringUtils.isNotBlank(requestParam.getCustomerAddress())) {
			criteria.andCustomerAddressLike("%"+requestParam.getCustomerAddress()+"%");
		}
		if(requestParam.getReserveEnable() != null) {
			criteria.andReserveEnableEqualTo(requestParam.getReserveEnable());
		}
		
		if(StringUtils.isNotBlank(requestParam.getServiceStartDate())) {
			Date date = DateUtils.parseDate(requestParam.getServiceStartDate());
			criteria.andNextServiceDateGreaterThanOrEqualTo(DateUtils.getBeginDate(date));
		}
		if(StringUtils.isNotBlank(requestParam.getServiceEndDate())) {
			Date date = DateUtils.parseDate(requestParam.getServiceEndDate());
			criteria.andNextServiceDateLessThanOrEqualTo(DateUtils.addSomeDays(DateUtils.getBeginDate(date),1));
		}
		//查询业务数据类型
		if(requestParam.getOrderStatus().equals(OrderStatusEnum.ALL.getCode().toString())) {//全部清单--只显示正常会员数据
			criteria.andCurrentNumGreaterThan(0)
			        .andReserveStatusNotEqualTo(ReserveStatusEnum.MEMBER_END.getCode());
		}
		Date currentDate = DateUtils.getCurrentDate();
		if(requestParam.getOrderStatus().equals(OrderStatusEnum.WAIT_RESERVE.getCode().toString())) {//到期待预约--只显示正常会员数据
			criteria.andCurrentNumGreaterThan(0)
					.andReserveStatusEqualTo(ReserveStatusEnum.UN_RESERVE.getCode())
					.andReserveEnableEqualTo(ReserveEnableEnum.OPEN.getCode())
			        .andNextServiceDateLessThan(DateUtils.addSomeDays(currentDate, 1));
		}
		if(requestParam.getOrderStatus().equals(OrderStatusEnum.WAIT_RESERVE_NOT_REMIND.getCode().toString())) {//到期待预约-不提醒
			criteria.andCurrentNumGreaterThan(0)
					.andReserveStatusEqualTo(ReserveStatusEnum.UN_RESERVE.getCode())
					.andReserveEnableEqualTo(ReserveEnableEnum.CLOSE.getCode())
			        .andNextServiceDateLessThan(DateUtils.addSomeDays(currentDate, 1));
		}
		if(requestParam.getOrderStatus().equals(OrderStatusEnum.COMING_SOON.getCode().toString())) {//明日服务清单--只显示正常会员数据
			criteria.andCurrentNumGreaterThan(0)
			        .andReserveStatusEqualTo(ReserveStatusEnum.RESERVE.getCode());
			        //.andNextServiceDateLessThan(DateUtils.addSomeDays(currentDate, 2));
		}
		if(requestParam.getOrderStatus().equals(OrderStatusEnum.EXCEPTION_RESERVE.getCode().toString())) {//预约异常--只显示正常会员数据
			criteria.andCurrentNumGreaterThan(0)
			        .andReserveStatusEqualTo(ReserveStatusEnum.RESERVE_ERROR.getCode());
		}
		if(requestParam.getOrderStatus().equals(OrderStatusEnum.MEMBER_EXPIRE.getCode().toString())) {//会员到期--只显示正常会员数据
			criteria.andCurrentNumLessThanOrEqualTo(0)
			        .andRenewalStatusEqualTo(requestParam.getRenewalStatus())
			        .andReserveStatusEqualTo(ReserveStatusEnum.MEMBER_END.getCode());
		}
		criteria.andStatusEqualTo(StatusEnum.ENABLE.getCode())
			   .andIsDeleteEqualTo(DeleteEnum.NORMAL.getCode());
		example.setOrderByClause("next_service_date asc");
		List<CustomerOrder> list = customerOrderMapper.selectByExample(example);
		PageInfo<CustomerOrder> page = new PageInfo<>(list);
		return page;
	}

}
